package com.docking.heylo.web.controller;

import com.docking.heylo.constant.SystemConstant;
import com.docking.heylo.dao.entity.AppVersion;
import com.docking.heylo.dao.entity.Dict;
import com.docking.heylo.dao.mapper.DictMapper;
import com.docking.heylo.service.IAppVersionService;
import com.docking.heylo.service.IUserService;
import com.docking.heylo.utils.HeyloMessageSource;
import com.docking.heylo.web.dto.EnrollmentDto;
import com.docking.heylo.web.dto.UserDto;
import com.docking.heylo.web.utils.ResultUtil;
import com.docking.heylo.web.vo.BaseVo;
import com.docking.heylo.web.vo.LoginResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;


/***
 * APP 用户登录相关控制器
 *
 */
@RestController
public class LoginController {

    @Autowired
    private IUserService userService;

    @Autowired
    private HeyloMessageSource ms;

    @Autowired
    private IAppVersionService versionService;

    @Resource
    private DictMapper dictMapper;



    /***
     * 发送注册验证码
     *
     * @param enrollmentDto
     * @return
     */
    @PostMapping("/sendEnrollmentVerCode")
    public BaseVo sendEnrollmentVerCode(@RequestBody @Validated(EnrollmentDto.SendVerCode.class) EnrollmentDto enrollmentDto){
        userService.sendEnrollmentVerCode(enrollmentDto);
        return ResultUtil.success(ms.getMessage(SystemConstant.success));
    }

    /***
     * 注册
     *
     * @return
     */
    @PostMapping("/enrollment")
    public BaseVo enrollment(@RequestBody @Validated(EnrollmentDto.SubmitEnrollment.class) EnrollmentDto enrollmentDto){
        LoginResult result = userService.enrollment(enrollmentDto);
        return ResultUtil.success(ms.getMessage("registration.success"), result);
    }

    /***
     * 登录-密码登录
     *
     * @param enrollmentDto
     * @return
     */
    @PostMapping("/submitPhoneAndPassword")
    public BaseVo submitPhoneAndPassword(@RequestBody @Validated(EnrollmentDto.Login.class) EnrollmentDto enrollmentDto){
        LoginResult loginResult = userService.login(enrollmentDto);
        return ResultUtil.success(ms.getMessage(SystemConstant.success), loginResult);
    }


    /***
     * 发送找回密码验证码
     *
     * @param user
     * @return
     */
    @PostMapping("/sendRetrievePassword")
    public BaseVo sendRetrievePassword(@RequestBody @Validated(UserDto.SendRetrievePasswordVerCode.class) UserDto user){
        userService.sendRetrievePassword(user);
        return ResultUtil.success(ms.getMessage(SystemConstant.success));
    }


    /***
     * 找回密码
     *
     * @param user
     * @return
     */
    @PostMapping("/retrievePassword")
    public BaseVo retrievePassword(@RequestBody @Validated({UserDto.RetrievePassword.class}) UserDto user){
        userService.retrievePassword(user);
        return ResultUtil.success(ms.getMessage(SystemConstant.success));
    }

    /***
     * 获取最新的APP版本
     *
     * @return
     */
    @PostMapping("/lastVersion")
    public BaseVo appVersion(@RequestBody AppVersion app){
        AppVersion version = versionService.lastVersion(app);
        return ResultUtil.success("成功", version);
    }


    /***
     * 环境
     *
     * @return
     */
    @PostMapping("/surroundings")
    public BaseVo surroundings(@RequestBody AppVersion version){
        String name = "surroundings_android";
        if(version.getType().intValue() == 1){
            name = "surroundings_android";
        }else if(version.getType().intValue() == 2){
            name = "surroundings_ios";
        }

        Dict dict = dictMapper.selectByParentAndName("app_config",name);
        return ResultUtil.success("成功", new HashMap<String, String>(){{
            put("surroundings", dict.getValue());
        }});
    }

    /***
     * 版本检查
     *
     * @param version
     * @return
     */
    @RequestMapping("/versionCheck")
    public BaseVo versionCheck(@RequestBody AppVersion version){
        String name = "surroundings_android";
        if(version.getType().intValue() == 1){
            name = "surroundings_android";
        }else if(version.getType().intValue() == 2){
            name = "surroundings_ios";
        }

        // 配置环境
        Dict dict = dictMapper.selectByParentAndName("app_config",name);

        // 版本信息
        AppVersion newVersion = versionService.lastVersion(version);

        return ResultUtil.success("成功", new HashMap<String, Object>(){{
            put("surroundings", dict.getValue());
            put("version", newVersion);
        }});
    }







}
